package com.yuan.rest.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.yuan.rest.controller.vo.UserInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author SiYuan
 * @Description :  测试 axios请求的数据
 *
 * @Date 2018/12/4 17:13
 */

@Controller
public class IndexController {
    @Value("${pathdir}")
    public String pathdir;
    @GetMapping("/index")
    @ResponseBody
    public Map<String, Object> index() {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < 20; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setAge("100" + i);
            userInfo.setName("zhangsan" + i);
            arrayList.add(userInfo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ststus", 200);
        map.put("data", arrayList);
        return map;
    }

    @GetMapping("/index1")
    public String index1(){
        return "index";
    }

    /***
     * 文件上传
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file ){
        Map<String, Object> map = new HashMap<>();
        String contentType = file.getContentType();  //图片文件类型
        String fileName = file.getOriginalFilename(); //图片名字
        String path = pathdir;
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String simpleUUID = IdUtil.simpleUUID();
        File dest = new File(path +simpleUUID+"."+suffix);
        try {
            file.transferTo(dest);
            map.put("status", 200);
            map.put("path", "upload/"+simpleUUID+"."+suffix);
            map.put("msg", "上传成功");
            return map;
        } catch (IOException e) {
        }
        return map;
    }

}
